package net.wofly.sale.order.web;

import com.alipay.api.AlipayApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.common.pay.util.ClientType;
import net.wofly.common.persistence.PageWrapper;
import net.wofly.common.web.BaseController;
import net.wofly.framework.util.ResultCode;
import net.wofly.framework.util.ReturnBean;
import net.wofly.sale.order.domain.Order;
import net.wofly.sale.order.domain.OrderStatusDto;
import net.wofly.sale.order.domain.OrderType;
import net.wofly.sale.order.dto.req.BanquetOrderReq;
import net.wofly.sale.order.dto.req.NoMoneyOrderReq;
import net.wofly.sale.order.dto.req.ReturnGoodsOrderReq;
import net.wofly.sale.order.dto.resp.ReturnGoodsDtoResp;
import net.wofly.sale.order.flow.IOrderFlowService;
import net.wofly.sale.order.service.IReturnSalesService;
import net.wofly.sale.order.service.ISaleOrderService;
import net.wofly.sale.order.util.OrderStatus;
import net.wofly.sale.order.util.PlaceOrderType;
import net.wofly.sale.order.validator.OrderValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/api/saleOrder")
@Api(description = "订单管理接口(商贸模块)")
public class SaleOrderController extends BaseController {
    
    private static final String DEFAULT_PAGE_NUM = "1";
    private static final String DEFAULT_PAGE_SIZE = "10";
    Logger logger = LoggerFactory.getLogger(SaleOrderController.class);
    @Autowired
    ISaleOrderService iSaleOrderService;
    @Autowired
    IOrderFlowService iOrderFlowService;
    @Autowired
    IReturnSalesService iReturnSalesService;
    
    /**
     * @param order
     */
    @ApiOperation(value = "创建订单")
    @ApiImplicitParams({@ApiImplicitParam(name = "order", value = "订单实体", required = true, dataType = "Order")})
    @RequestMapping(value = "/createOrder", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<Order> createOrder(
            @RequestBody Order order) throws CloneNotSupportedException, IOException, AlipayApiException {
        ReturnBean<Order> returnBean;
        String errorMessage = OrderValidator.validateCreateOrderParams(order);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<Order>(-1, errorMessage);
        } else {
            returnBean = iSaleOrderService.createOrder(order);
            if (ResultCode.SUCCESS.intValue() == returnBean.getCode() && net.wofly.sale.order.util.OrderType.会员订单
                    .getValue().intValue() != returnBean.getResult().getOrderType()) {
                Order ord = returnBean.getResult();
                ReturnBean<String> returnBeanWorkFlow = new ReturnBean<>(ResultCode.ERROR_SERVICE, "处理失败");
                // 内欠和还货订单是创建订单时直接启动工作流，其他在看支付信息
                if (net.wofly.sale.order.util.OrderType.无款订单.getValue().intValue() == order.getOrderType()) {
                    //无款订单，订单提交_未支付 --> 老板审核
                    returnBeanWorkFlow = iOrderFlowService.startWorkflow(order.getBuyer(), ord.getOrderID(),
                            OrderStatus.订单提交_未支付.getValue().toString(), null);
                } else if (net.wofly.sale.order.util.OrderType.还货订单.getValue().intValue() == order.getOrderType()) {
                    // 还货订单，订单己支付 --> 出纳平账
                    returnBeanWorkFlow = iOrderFlowService.startWorkflow(order.getBuyer(), ord.getOrderID(),
                            OrderStatus.订单己支付.getValue().toString(), null);
                } else {
                    if (PlaceOrderType.代客下单.getValue().intValue() == order.getPlaceOrderType()) {
                        if (null != order.getPayType()) {
                            //订单创建成功，生成支付信息，同时调用工作流启动工作流
                            returnBeanWorkFlow = iSaleOrderService.pay(order.getOrderID(), order.getPayType(),
                                    ClientType.PC端.getValue().intValue(),
                                    order.getPayAmount());
                        }
                    } else {
                        return returnBean;
                    }
                }
                /* else if (OrderStatus.订单己支付.getValue().intValue() == ord.getStatus()
                        || OrderStatus.支付定金.getValue().intValue() == ord.getStatus()
                        || OrderStatus.白条已支付.getValue().intValue() == ord.getStatus()
                        || OrderStatus.货到付款_未支付.getValue().intValue() == ord.getStatus()) {
                    returnBeanWorkFlow = iOrderFlowService.startWorkflow(order.getBuyer(), ord.getOrderID(),
                            String.valueOf(ord.getStatus()), null);
                }*/
                
                if (returnBeanWorkFlow.getCode().intValue() != ResultCode.SUCCESS) {
                    return new ReturnBean<>(returnBeanWorkFlow.getCode(),
                            returnBeanWorkFlow.getMsg(), returnBean.getResult());
                }
            }
        }
        return returnBean;
    }
    
    /**
     * 获取客户订单，包括客户自己下单及销售代为下单的所有订单
     * <ul>
     * <li> </li>
     * </ul>
     *
     * @param customerID
     */
    @ApiOperation(value = "获取客户订单，包括客户自己下单及销售代为下单的所有订单")
    @ApiImplicitParams({@ApiImplicitParam(name = "customerID", value = "客户ID", required = true, dataType = "string",
            paramType = "form"), @ApiImplicitParam(name = "pageNum", value = "页码（默认第一页）", required = true, dataType =
            "int", paramType = "form"), @ApiImplicitParam(name = "pageSize", value = "每页显示条数（默认10条）", required =
            true, dataType = "int", paramType = "form")})
    @RequestMapping(value = "/getCustomerOrder", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<PageWrapper<Order>> getCustomerOrder(
            String customerID, @RequestParam(defaultValue = DEFAULT_PAGE_NUM) Integer pageNum,
            @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) Integer pageSize) {
        ReturnBean<PageWrapper<Order>> returnBean;
        String errorMessage = OrderValidator.validateCustomerID(customerID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<PageWrapper<Order>>(-1, errorMessage);
        } else {
            returnBean = iSaleOrderService.getCustomerOrder(customerID, pageNum, pageSize);
        }
        return returnBean;
    }
    
    /**
     * @param orderID
     */
    @ApiOperation(value = "获取订单详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderID", value = "订单ID", required = true, dataType = "string",
            paramType = "form")})
    @RequestMapping(value = "/getOrder", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<Order> getOrder(String orderID) {
        ReturnBean<Order> returnBean;
        String errorMessage = OrderValidator.validateOrderID(orderID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<Order>(-1, errorMessage);
        } else {
            returnBean = iSaleOrderService.getOrder(orderID);
        }
        return returnBean;
    }
    
    /**
     * 获取销售订单
     * <ul>
     * <li>是指定销售所管理的所有客户订单。</li>
     * <li>需要先获取所有其管理的客户列表，然后取得所有订单</li>
     * <li>placeOrderType 不传时，为所有订单</li>
     * <li>customer 不传时为所有客户</li>
     * <li></li>
     * </ul>
     *
     * @param salerID
     * @param orderNo
     * @param orderType
     * @param customerName
     * @param orderType
     * @param status
     */
    @ApiOperation(value = "获取销售员的订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "salerID", value = "员工ID（销售员）", required = true, dataType = "string", paramType
                    = "form"),
            @ApiImplicitParam(name = "orderNo", value = "订单号", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "orderType", value = "订单类型 1:会员订单;2:团购订单;3:流通订单;4:酒店订单;5:签单订单;6:宴会订单;7:订货会订单(流通)" +
                    ";8:订货会订单(酒店);9:还货订单;10:无款订单", allowableValues = "1,2,3,4,5,6,7,8,9,10", dataType = "int",
                    paramType = "form"),
            @ApiImplicitParam(name = "customerName", value = "客户姓名 ", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "status", value = "订单状态", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageNum", value = "页码（默认第一页）", required = true, dataType = "int", paramType =
                    "form"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数（默认10条）", required = true, dataType = "int",
                    paramType = "form")})
    @RequestMapping(value = "/getSalerOrder", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<PageWrapper<Order>> getSalerOrder(
            String salerID, String orderNo, Integer orderType, String customerName, Integer status,
            @RequestParam(defaultValue = DEFAULT_PAGE_NUM) Integer pageNum,
            @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) Integer pageSize) {
        ReturnBean<PageWrapper<Order>> returnBean;
        String errorMessage = OrderValidator.validateEmployeeID(salerID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<PageWrapper<Order>>(-1, errorMessage);
        } else {
            returnBean = iSaleOrderService.getSalerOrder(salerID, orderNo, orderType, customerName, status, pageNum,
                    pageSize);
        }
        return returnBean;
    }
    
    /**
     * 根据条件获取订单
     * <ol>
     * <li></li>
     * </ol>
     *
     * @param orderNo
     * @param orderType
     * @param customerName
     * @param status
     * @param pageNum
     * @param pageSize
     */
    @ApiOperation(value = "根据条件获取订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNo", value = "订单号", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "orderType", value = "订单类型 1:会员订单;2:团购订单;3:流通订单;4:酒店订单;5:签单订单;6:宴会订单;7:订货会订单(流通)" +
                    ";8:订货会订单(酒店);9:还货订单;10:无款订单", allowableValues = "1,2,3,4,5,6,7,8,9,10", dataType = "int",
                    paramType = "form"),
            @ApiImplicitParam(name = "customerName", value = "客户姓名", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "status", value = "订单状态", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "Long", paramType = "form"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "Long", paramType = "form"),
            @ApiImplicitParam(name = "pageNum", value = "页码（默认第一页）", required = true, dataType = "int", paramType =
                    "form"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数（默认10条）", required = true, dataType = "int",
                    paramType = "form")})
    @RequestMapping(value = "/getOrderByCnd", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<PageWrapper<Order>> getOrderByCnd(
            String orderNo, Integer orderType, String customerName, Integer status, Long startTime, Long endTime,
            @RequestParam(defaultValue = DEFAULT_PAGE_NUM) Integer pageNum,
            @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) Integer pageSize) {
        ReturnBean<PageWrapper<Order>> returnBean;
        String errorMessage = "";
        //OrderValidator.validateEmployeeID(salerID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<PageWrapper<Order>>(-1, errorMessage);
        } else {
            returnBean = iSaleOrderService.getOrderByCnd(orderNo, orderType, customerName, status, startTime, endTime,
                    pageNum, pageSize);
        }
        return returnBean;
    }
    
    
    /**
     * @param userID
     * @param order
     */
    @ApiOperation(value = "获取确认订单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userID", value = "用户ID", required = true, dataType = "string", paramType =
                    "form"),
            @ApiImplicitParam(name = "order", value = "订单", required = true, dataType = "string", paramType = "form")
    })
    @RequestMapping(value = "/getVerifyOrder", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<Order> getVerifyOrder(String userID, String order) throws CloneNotSupportedException {
        ReturnBean<Order> returnBean;
        String errorMessage = OrderValidator.validateGetVerifyOrderParams(userID, order);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iSaleOrderService.getVerifyOrder(userID, order);
        }
        return returnBean;
    }
    
    /**
     * 获取所有订单类型
     *
     * @return
     */
    @ApiOperation(value = "获取所有订单类型")
    @RequestMapping(value = "/getAllOrderTypes", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<List<OrderType>> getAllOrderTypes() {
        ReturnBean<List<OrderType>> returnBean = iSaleOrderService.getAllOrderTypes();
        return returnBean;
    }
    
    /**
     * 根据角色获取订单类型
     *
     * @param roleID
     */
    @ApiOperation(value = "根据角色获取订单类型")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleID", value = "角色ID", required = true, dataType = "string",
            paramType = "form")})
    @RequestMapping(value = "/getOrderTypeByRole", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<List<OrderType>> getOrderTypeByRole(String roleID) throws CloneNotSupportedException {
        ReturnBean<List<OrderType>> returnBean;
        String errorMessage = OrderValidator.validateRoleID(roleID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iSaleOrderService.getOrderTypeByRole(roleID);
        }
        return returnBean;
    }
    
    /**
     * 获取订单状态，订单查询页面搜索条件用
     */
    @ApiOperation(value = "获取订单状态，订单查询页面搜索条件用")
    @RequestMapping(value = "/getOrderStatus", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<List<OrderStatusDto>> getOrderStatus() {
        ReturnBean<List<OrderStatusDto>> returnBean = iSaleOrderService.getOrderStatus();
        return returnBean;
    }
    
    /**
     * 更改订单类型
     *
     * @param orderType
     * @param order
     * @return
     */
    @ApiOperation(value = "更改订单类型")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderType", value = "订单类型 1:会员订单;2:团购订单;3:流通订单;4:酒店订单;5:签单订单;" +
            "6:宴会订单;7:订货会订单(流通);8:订货会订单(酒店);9:还货订单;10:无款订单", allowableValues = "1,2,3,4,5,6,7,8,9,10", required =
            true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "order", value = "订单实体", required = true, dataType = "Order", paramType = "body")})
    @RequestMapping(value = "/changeOrderType", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<Order> changeOrderType(
            Integer orderType, @RequestBody Order order) throws CloneNotSupportedException {
        ReturnBean<Order> returnBean;
        String errorMessage = OrderValidator.validateChangeOrderTypeParams(orderType, order);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<Order>(-1, errorMessage);
        } else {
            returnBean = iSaleOrderService.changeOrderType(orderType, order);
        }
        return returnBean;
    }
    
    @RequestMapping(value = "/success", method = {RequestMethod.GET})
    public ModelAndView paySuccess() {
        logger.info(">>> 支付 成功");
        return new ModelAndView("paySuccess");
    }
    
    @RequestMapping(value = "/order", method = {RequestMethod.GET})
    public ModelAndView order() {
        logger.info(">>> 进入订单页面 ");
        return new ModelAndView("order");
    }
    
    @RequestMapping(value = "/order1", method = {RequestMethod.GET, RequestMethod.POST})
    public String order1() {
        logger.info(">>> 进入订单页面1 ");
        return "order1";
    }
    
    /**
     * @param request
     * @param userID
     * @param orderID
     * @param orderStatus 当发起人后面是决策节点时，需要传递一个 flow_opt 值工作流来决定下一步工作流走向
     * @return
     */
    @ApiOperation(value = "发起订单工作流", notes = "发起订单工作流")
    @ApiImplicitParams({@ApiImplicitParam(name = "userID", value = "申请人ID", required = true, dataType = "String",
            paramType = "query"),
            @ApiImplicitParam(name = "orderID", value = "订单ID", required = true, dataType =
                    "String", paramType = "query"),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态", required = true,
                    dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/startWorkflow", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<String> startWorkflow(
            HttpServletRequest request, String userID, String orderID, String orderStatus) {
        String errorMessage = OrderValidator.validateStartWorkFlow(userID, orderID, orderStatus);
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        
        Map<String, Object> variables = new HashMap<>();
        return iOrderFlowService.startWorkflow(userID, orderID, orderStatus, variables);
    }
    
    @ApiOperation(value = "获取订单待办", notes = "获取订单待办")
    @ApiImplicitParams({@ApiImplicitParam(name = "userID", value = "用户ID", required = true, dataType = "String",
            paramType = "query")
    })
    @RequestMapping(value = "/findToDoList", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<List<Order>> findToDoList(String userID) {
        String errorMessage = OrderValidator.validateUserID(userID);
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        
        List<Order> orders = iSaleOrderService.findToDoList(userID);
        
        return new ReturnBean<>(0, "获取订单待办任务成功！", orders);
        
    }
    
    @ApiOperation(value = "完成订单工作流任务", notes = "完成订单工作流任务")
    @ApiImplicitParams({@ApiImplicitParam(name = "userID", value = "用户ID", required = true, dataType = "String",
            paramType = "query"),
            @ApiImplicitParam(name = "taskID", value = "任务ID", required = true, dataType = "String", paramType =
                    "query"),
            @ApiImplicitParam(name = "comment", value = "任务审批意见", required = false, dataType = "String", paramType =
                    "query")
    })
    @RequestMapping(value = "/complete", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<String> complete(HttpServletRequest request, String userID, String taskID, String comment) {
        Map<String, String[]> mapRequest = request.getParameterMap();
        
        String errorMessage = OrderValidator.complete(userID, taskID);
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        
        return iOrderFlowService.complete(userID, taskID, comment, mapRequest);
    }
    
    @ApiOperation(value = "为工作流获取订单详情", notes = "为工作流获取订单详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderID", value = "订单ID", required = true, dataType = "String",
            paramType = "query")
    })
    @RequestMapping(value = "/getOrderForFlow", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<Order> getOrderForFlow(String orderID) {
        String errorMessage = OrderValidator.validateOrderID(orderID);
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        
        return iSaleOrderService.getOrderForFlow(orderID);
    }
    
    @RequestMapping(value = "/claimTask", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "userID", value = "用户ID", required = true, dataType = "String",
            paramType = "form"),
            @ApiImplicitParam(name = "taskID", value = "任务ID", required = true, dataType = "String", paramType = "form")
    })
    @ResponseBody
    public ReturnBean<String> claimTask(String userID, String taskID) {
        
        String errorMessage = OrderValidator.complete(userID, taskID);
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        
        return iOrderFlowService.claimTask(userID, taskID);
    }
    
    @RequestMapping(value = "/unclaimTask", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "userID", value = "用户ID", required = true, dataType = "String",
            paramType = "form"),
            @ApiImplicitParam(name = "taskID", value = "任务ID", required = true, dataType = "String", paramType = "form")
    })
    @ResponseBody
    public ReturnBean<String> unclaimTask(String userID, String taskID) {
        
        String errorMessage = OrderValidator.complete(userID, taskID);
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        
        return iOrderFlowService.unclaimTask(userID, taskID);
    }
    
    /**
     * 获取待还货客户的商品库存
     *
     * @param customerUserID
     * @return
     */
    @ApiOperation(value = "获取待还货客户的商品库存")
    @RequestMapping(value = "/getCustomerReturnGoodsStock", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "customerUserID", value = "用户ID", required = true, dataType =
            "String", paramType = "form")})
    @ResponseBody
    public ReturnBean<List<ReturnGoodsDtoResp>> getCustomerReturnGoodsStock(String customerUserID) {
        String errorMessage = OrderValidator.validateCustomerID(customerUserID);
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        return iSaleOrderService.getCustomerReturnGoodsStock(customerUserID);
    }
    
    /**
     * 创建订单（还货订单专用）
     *
     * @param returnGoodsOrderReq
     * @return
     */
    @ApiOperation(value = "创建订单（还货订单专用）")
    @RequestMapping(value = "/createOrderForReturnGoods", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "returnGoodsOrderReq", value = "还货订单输入参数", required = true, dataType =
            "ReturnGoodsOrderReq", paramType = "body")})
    @ResponseBody
    public ReturnBean<Order> createOrderForReturnGoods(@RequestBody ReturnGoodsOrderReq returnGoodsOrderReq) {
        String errorMessage = OrderValidator.validateCreateOrderForReturnGoodsParams(returnGoodsOrderReq);
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        ReturnBean<Order> returnBean = iSaleOrderService.createOrderForReturnGoods(returnGoodsOrderReq);
        if (ResultCode.SUCCESS.intValue() == returnBean.getCode()) {
            Order order = returnBean.getResult();
            ReturnBean<String> returnBean1 = iOrderFlowService.startWorkflow(returnGoodsOrderReq.getBuyerID(),
                    order.getOrderID(),
                    String.valueOf(order.getStatus()), null);
            return new ReturnBean<>(returnBean1.getCode(), returnBean1.getMsg(), order);
        }
        return returnBean;
    }
    
    /**
     * 创建订单（无款订单专用），只是为了销库存
     *
     * @param noMoneyOrderReq
     * @return
     */
    @ApiOperation(value = "创建订单（无款订单专用）")
    @RequestMapping(value = "/createNoMoneyOrder", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "noMoneyOrderReq", value = "无款订单输入参数", required = true, dataType =
            "NoMoneyOrderReq", paramType = "body")})
    @ResponseBody
    public ReturnBean<Order> createNoMoneyOrder(@RequestBody NoMoneyOrderReq noMoneyOrderReq) {
        String errorMessage = OrderValidator.validateCreateNoMoneyOrderarams(noMoneyOrderReq);
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        return iSaleOrderService.createNoMoneyOrder(noMoneyOrderReq);
    }
    
    /**
     * 修改订单（无款订单专用），只是为了销库存
     *
     * @return
     */
    @ApiOperation(value = "修改订单（无款订单专用）")
    @RequestMapping(value = "/updateNoMoneyOrder", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "order", value = "订单对象", required = true, dataType =
            "Order", paramType = "body")})
    @ResponseBody
    public ReturnBean<Order> updateNoMoneyOrder(@RequestBody Order order) {
        String errorMessage = OrderValidator.validateUpdateNoMoneyOrderParams(order);
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        return iSaleOrderService.updateNoMoneyOrder(order);
    }
    
    /**
     * 删除订单（无款订单专用）
     *
     * @return
     */
    @ApiOperation(value = "删除订单（无款订单专用）")
    @RequestMapping(value = "/deleteNoMoneyOrder", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "orderID", value = "订单ID", required = true, dataType =
            "order", paramType = "form")})
    @ResponseBody
    public ReturnBean<String> deleteNoMoneyOrder(String orderID) {
        ReturnBean<String> returnBean;
        String errorMessage = OrderValidator.validateOrderID(orderID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<String>(-1, errorMessage);
        } else {
            returnBean = iSaleOrderService.deleteNoMoneyOrder(orderID);
        }
        return returnBean;
    }
    
    /**
     * 创建宴会订单（宴会订单专用）
     *
     * @return
     */
    @ApiOperation(value = "创建宴会订单（宴会订单专用）")
    @RequestMapping(value = "/createBanquetOrder", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "banquetOrderReq", value = "宴会订单输入参数", required = true, dataType =
            "BanquetOrderReq", paramType = "body")})
    @ResponseBody
    public ReturnBean<Order> createBanquetOrder(
            @RequestBody BanquetOrderReq banquetOrderReq) throws CloneNotSupportedException {
        String errorMessage = OrderValidator.validateCreateBanquetOrderarams(banquetOrderReq);
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        return iSaleOrderService.createBanquetOrder(banquetOrderReq);
    }
    
    
    /**
     * 删除宴会订单（宴会订单专用）
     *
     * @return
     */
    @ApiOperation(value = "删除宴会订单（宴会订单专用）")
    @RequestMapping(value = "/deleteBanquetOrder", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "orderID", value = "订单ID", required = true, dataType =
            "order", paramType = "form")})
    @ResponseBody
    public ReturnBean<String> deleteBanquetOrder(String orderID) {
        ReturnBean<String> returnBean;
        String errorMessage = OrderValidator.validateOrderID(orderID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<String>(-1, errorMessage);
        } else {
            returnBean = iSaleOrderService.deleteBanquetOrder(orderID);
        }
        return returnBean;
    }
    
    /**
     * 获取销售订单
     * <ul>
     * <li>是指定销售所管理的所有客户订单。</li>
     * <li>需要先获取所有其管理的客户列表，然后取得所有订单</li>
     * <li>placeOrderType 不传时，为所有订单</li>
     * <li>customer 不传时为所有客户</li>
     * <li></li>
     * </ul>
     *
     * @param userID
     * @param orderNo
     * @param orderType
     * @param customerName
     * @param orderType
     * @param status
     */
    @ApiOperation(value = "获取我的订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userID", value = "用户ID（业务员或者客户）", required = true, dataType = "string", paramType
                    = "form"),
            @ApiImplicitParam(name = "orderNo", value = "订单号", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "orderType", value = "订单类型 1:会员订单;2:团购订单;3:流通订单;4:酒店订单;5:签单订单;6:宴会订单;7:订货会订单(流通)" +
                    ";8:订货会订单(酒店);9:还货订单;10:无款订单", allowableValues = "1,2,3,4,5,6,7,8,9,10", dataType = "int",
                    paramType = "form"),
            @ApiImplicitParam(name = "customerName", value = "客户姓名 ", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "status", value = "订单状态", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageNum", value = "页码（默认第一页）", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数（默认10条）", dataType = "int", paramType = "form")})
    @RequestMapping(value = "/getMyOrders", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<PageWrapper<Order>> getMyOrders(
            String userID, String orderNo, Integer orderType, String customerName, Integer status,
            @RequestParam(defaultValue = DEFAULT_PAGE_NUM) Integer pageNum,
            @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) Integer pageSize) {
        ReturnBean<PageWrapper<Order>> returnBean;
        String errorMessage = OrderValidator.validateUserID(userID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iSaleOrderService.getMyOrders(userID, orderNo, orderType, customerName, status, pageNum,
                    pageSize);
        }
        return returnBean;
    }
    
    @ApiOperation(value = "取消订单")
    @RequestMapping(value = "/cancelOrder", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "orderID", value = "订单ID", required = true, dataType = "String",
            paramType = "form")})
    @ResponseBody
    public ReturnBean<String> cancelOrder(String orderID) {
        ReturnBean<String> returnBean;
        String errorMessage = OrderValidator.validateOrderID(orderID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iSaleOrderService.cancelOrder(orderID, null);
        }
        return returnBean;
    }
    
    @ApiOperation(value = "根据状态获取我的订单，用于移动端")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userID", value = "用户ID（业务员或者客户）", required = true, dataType = "string", paramType
                    = "form"),
            @ApiImplicitParam(name = "orderNo", value = "订单号", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "orderType", value = "订单类型 1:会员订单;2:团购订单;3:流通订单;4:酒店订单;5:签单订单;6:宴会订单;7:订货会订单(流通)" +
                    ";8:订货会订单(酒店);9:还货订单;10:无款订单", allowableValues = "1,2,3,4,5,6,7,8,9,10", dataType = "int",
                    paramType = "form"),
            @ApiImplicitParam(name = "customerName", value = "客户姓名 ", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "status", value = "订单状态(只支持4种状态定义：-1：全部；1：待支付；2：已支付；9:取消)", allowableValues = "-1,1,2,9", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageNum", value = "页码（默认第一页）", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数（默认10条）", dataType = "int", paramType = "form")})
    @RequestMapping(value = "/getMyOrdersByStatus", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<PageWrapper<Order>> getMyOrdersByStatus(
            String userID, String orderNo, Integer orderType, String customerName,
            @RequestParam(defaultValue = "-1") Integer status,
            @RequestParam(defaultValue = DEFAULT_PAGE_NUM) Integer pageNum,
            @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) Integer pageSize) {
        ReturnBean<PageWrapper<Order>> returnBean;
        String errorMessage = OrderValidator.validateUserID(userID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iSaleOrderService.getMyOrdersByStatus(userID, orderNo, orderType, customerName, status, pageNum,
                    pageSize);
        }
        return returnBean;
    }
    
}
